﻿using MediatR;
using Microsoft.Identity.Client.Extensions.Msal;
using System;
using W.Common.Enum;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.MoveCommand;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.MoveCommandHander
{
    public class MoveFanCommandHandler : IRequestHandler<MoveFanCommand, ApiResult<FanMoveDTO>>
    {
        private readonly IMoveProductRepository moveProductRepository;
        private readonly IMoveTheLibraryRepository moveTheLibraryRepository;
        public MoveFanCommandHandler(IMoveProductRepository moveProductRepository, IMoveTheLibraryRepository moveTheLibraryRepository)
        {
            this.moveProductRepository = moveProductRepository;
            this.moveTheLibraryRepository = moveTheLibraryRepository;
        }

        public async Task<ApiResult<FanMoveDTO>> Handle(MoveFanCommand request, CancellationToken cancellationToken)
        {
            ApiResult<FanMoveDTO> result = new ApiResult<FanMoveDTO>();
            try
            {
                var products = await moveProductRepository.GetAllAsync();
                var move = from a in await moveTheLibraryRepository.GetAllAsync()
                           where a.IsDelete == false && a.MoveTheLibraryId ==
                           request.moveTheLibraryId
                           select new FanMoveDTO
                           {
                               MoveTheLibraryId = a.MoveTheLibraryId,
                               MoveTheLibraryNo = a.MoveTheLibraryNo,
                               MoveTheLibraryType = a.MoveTheLibraryType,
                               AssociateNumber = a.AssociateNumber,
                               SingleMaker = a.SingleMaker,
                               AuditStatus = a.AuditStatus,
                               AuditStatusTest = a.AuditStatus.ToString(),
                               MakeTime = a.MakeTime,
                               MoveRemark = a.MoveRemark,
                               ExamineReason = a.ExamineReason,
                               ExaminePeople = a.ExaminePeople,
                               ExamineTime = a.ExamineTime,
                               
                               moveProductData = products.Where(x => x.MoveTheLibraryId == a.MoveTheLibraryId).Select(x => new MoveProductData
                               {
                                   MoveProductId = x.MoveProductId,
                                   ProductName = x.ProductName,
                                   ProductNo = x.ProductNo,
                                   ProductSpec = x.ProductSpec,
                                   Batch = x.Batch,
                                   Price = x.Price,
                                   OutStorageNum = x.OutStorageNum,
                                   NewBinLocationName = x.NewBinLocationName,
                                   BinLocationId = x.BinLocationId,
                                   MoveTheLibraryId = x.MoveTheLibraryId,
                                   WarehouseLocationName=x.WarehouseLocationName,
                                   WarehouseName = x.WarehouseName,
                               }).ToList()

                           };
                result.Data = move.FirstOrDefault();
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
